#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SINGLE_STR_LEN 100

typedef struct {
    int index;
    char *str;
} SingleStr;

int cmp_char(const void *a, const void *b)
{
    return *(char *)a - *(char *)b;
}

int cmp_dictionary(const void *a, const void *b)
{
    return strcmp(((SingleStr *)a)->str, ((SingleStr *)b)->str);
}

char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){
    /* arrays sort in dictionary*/
    char buf[strsSize][MAX_SINGLE_STR_LEN];
    SingleStr *strBuf = (SingleStr *)malloc(sizeof(SingleStr) * strsSize);
    for (int i = 0; i < strsSize; i++) {
        strcpy(buf[i], strs[i]);
        strBuf[i].index = i;
        strBuf[i].str = buf[i];
        qsort(strBuf[i].str, strlen(strBuf[i].str), sizeof(char), cmp_char);
    }
    qsort(strBuf, strsSize, sizeof(SingleStr), cmp_dictionary);

    /* malloc retStr memory and retColSize memory */
    int rowNum = 0, colNum = 0;
    *returnColumnSizes = (int *)malloc(sizeof(int) * strsSize);
    char ***retStr = (char ***)malloc(sizeof(char **) * strsSize);

    retStr[rowNum] = (char **)malloc(sizeof(char *) * strsSize);
    int left = 0;
    for (int right = 0; right < strsSize; right++) {
        if (!strcmp(strBuf[left].str, strBuf[right].str)) { /* Inter loop if arrays compare quel. */
            retStr[rowNum][colNum] = (char *)malloc(strlen(strs[strBuf[right].index]) + 1);
            strcpy(retStr[rowNum][colNum], strs[strBuf[right].index]);
            colNum++;
            continue;
        }
        (*returnColumnSizes)[rowNum] = colNum;
        left = right;

        /* create new column and new rowStr*/
        colNum = 0;
        rowNum++;
        retStr[rowNum] = (char **)malloc(sizeof(char *) * strsSize);
        retStr[rowNum][colNum] = (char *)malloc(strlen(strs[strBuf[right].index]) + 1);
        strcpy(retStr[rowNum][colNum], strs[strBuf[right].index]);
        colNum++;
    }
    (*returnColumnSizes)[rowNum] = colNum;
    rowNum++;

    *returnSize = rowNum;
    free(strBuf);
    return retStr;
}

int main()
{
	
	return 0;
 } 
